<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户订单预览</title>

    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script th:src="@{/static/main.js}"></script>

</head>
<body>

<!--<div th:insert="publicHeader"/>-->
<!--<script th:src="@{/publicHeader.js}"></script>-->


<div class="container-fluid" id="app" style="margin-top: 180px;margin-bottom: 100px">


    <div class="container">
        <div class="row" >
            <div class="col-md-8">
                <select class="form-control" name="address" v-model="defaultAddressId" @change="changeAddress">
                    <option v-for="a in addressList" :value="a.id" >{{a.province}}{{a.city}}{{a.area}}{{a.detailAddress}}  {{a.tel}}   {{a.reciver}}</option>
                </select>
            </div>
            <div class="col-md-4">
                <button class="btn btn-primary" data-toggle="modal" data-target="#myModal" @click="clearAddForm">新增地址</button>
            </div>
        </div>
    </div>
    <br/>

    <div class="row">
        <div v-if="cartVoList.length>0" style="margin-top: 10px;text-align: center"><h1>结　算</h1></div>
        <div class="container" v-for="cartVo in cartVoList">
            <div class="row">
                <img :src="cartVo.storeImg" style="width: 60px;height: 60px; border-radius: 50%"/>
                {{cartVo.storeName}}
            </div>
            <div class="row">
                <div class="col-md-12">
                    <table class="table">
                        <tr>
                            <th>编号</th>
                            <th>商品名</th>
                            <th>商品图片</th>
                            <th>图书单价(元)</th>
                            <th>购买数量</th>
                            <th>小计</th>
                        </tr>


                        <tr v-for="cartItem in cartVo.cartItemList">
                            <td style="height:60px;line-height: 60px" v-text="cartItem.productId">编号</td>
                            <td style="height:60px;line-height: 60px" v-text="cartItem.productName">名称</td>
                            <td>
                                <img :src="cartItem.productImg" style="width: 60px;height: 60px; border-radius: 50%"/>
                            </td>
                            <td style="height:60px;line-height: 60px" v-text="cartItem.productPrice">单价</td>
                            <td style="height:60px;line-height: 60px">
                                &nbsp;<span v-text="cartItem.buyCount"></span>&nbsp;
                            </td>
                            <td style="height:60px;line-height: 60px" v-text="cartItem.sumPrice">图书小计</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">新增地址</h4>
                </div>
                <div class="modal-body">
                    <form>
                        <div class="form-group">
                            <label for="provinceId">省</label>
                            <select class="form-control" id="provinceId" placeholder="请选择省份" v-model="provinceId" @change="refreshCity">
                                <option value="-1">请选择省份</option>
                                <option v-for="province in provinceList" :value="province.id" v-text="province.name"></option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="cityId">市</label>
                            <select class="form-control" id="cityId" placeholder="请选择城市" v-model="cityId" @change="refreshCounty">
                                <option value="-1">请选择城市</option>
                                <option v-for="city in cityList" :value="city.id" v-text="city.name"></option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="areaId">区</label>
                            <select class="form-control" id="areaId" placeholder="请选择区域"  v-model="countyId">
                                <option value="-1">请选择区域</option>
                                <option v-for="county in countyList" :value="county.id" v-text="county.name"></option>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="detailAddress">详细地址</label>
                            <input type="text" v-model="detail" class="form-control" id="detailAddress" placeholder="请输入详细地址">
                        </div>

                        <div class="form-group">
                            <label for="tel">手机号</label>
                            <input type="text" v-model="phoneNumber" class="form-control" id="tel" placeholder="请输入手机号">
                        </div>

                        <div class="form-group">
                            <label for="receiver">收件人姓名</label>
                            <input type="text" v-model="receiver" class="form-control" id="receiver" placeholder="请输入收件人">
                        </div>
                        <div class="form-group">
                            <label for="code">邮编</label>
                            <input type="text" v-model="mailCode" class="form-control" id="code" placeholder="请输入邮编">
                        </div>
                        <div class="checkbox">
                            <label>
                                <input type="checkbox" v-model="isDefault" id="isDefault" > 是否默认
                            </label>
                        </div>
                    </form>

                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" @click.prevent="saveAddress">保存</button>
                </div>
            </div>
        </div>
    </div>



</div>

<script>

    console.log(".........userorderpreview...........")

    new Vue({
        el: "#app",
        data: {
            // 地址
            addressList:[],
            defaultAddressId:-1,
            provinceList:[],
            provinceId:-1,
            cityList:[],
            cityId:-1,
            countyList:[],
            countyId:-1,
            detail:'',
            phoneNumber:'',
            receiver:'',
            mailCode:'',
            isDefault:'',
            // 结算详情
            productIds:[],
            cartVoList:'',
            totalPrice:"0.00",


        }
        ,
        methods: {
            //根据productIds读取要结算的项目
            //向后端发送请求,获取要结算项目的详情,在页面上显示
            initCartAll(){
                let _this=this;
                //根据productIds 获取结算页详情
                this.productIds=parent.app._data.productIds;
                $.post("/cart/calPrice",{productIds: this.productIds},function (data) {
                    console.log(data)
                    _this.totalPrice = data.totalPrice;
                    _this.cartVoList = data.cartVoList;
                })
            }
            ,
            initAddressList(){
                //去后端获取当前用户的所有收货地址
                let _this = this;
                $.get("/address/all",function (data) {
                    console.log(data)
                    _this.addressList = data;

                    //确认默认地址的id
                    for(let address of data){
                        if(address.isDefault == '1'){
                            _this.defaultAddressId = address.id;
                            break;
                        }
                    }
                    _this.changeAddress()
                })
            }
            ,
            initProvinceList(){  //省 / 直辖市
                let _this = this;

                $.get("/china/queryList?pid=0",function (data) {
                    console.log(data)

                    _this.provinceList = data;

                    //市 区 选择项要置空
                    _this.cityList=[];
                    _this.cityId = -1;
                    _this.countyList = [];
                    _this.countyId = -1;
                })
            }
            ,
            refreshCity(){   //市  / 直辖市的区
                console.log(this.provinceId)

                let _this = this;

                $.get("/china/queryList?pid="+this.provinceId,function (data) {
                    console.log(data)

                    _this.cityList = data;

                    //把县数据置空
                    _this.countyList = [];
                    _this.countyId = -1;
                })
            }
            ,
            refreshCounty(){   // 县 / -
                console.log(this.cityId)

                let _this = this;

                $.get("/china/queryList?pid="+this.cityId,function (data) {
                    console.log(data)

                    _this.countyList = data;
                })
            }
            ,
            saveAddress(){
                //搜集表单数据
                //省市区要id转name
                let provinceName = "";
                for(let item of this.provinceList){
                    if (item.id == this.provinceId){
                        provinceName = item.name;
                    }
                }

                let cityName = "";
                for(let item of this.cityList){
                    if (item.id == this.cityId){
                        cityName = item.name;
                    }
                }

                let countyName = "";
                for(let item of this.countyList){
                    if (item.id == this.countyId){
                        countyName = item.name;
                    }
                }


                //是否more地址 要 布尔值转字符串
                let defaultFlag = this.isDefault ? 1 : 0;

                let params = {
                    province:provinceName,
                    city:cityName,
                    area:countyName,
                    detailAddress:this.detail,
                    tel:this.phoneNumber,
                    receiver:this.receiver,
                    emailCode:this.mailCode,
                    isDefault:defaultFlag
                }

                console.log(params);

                let _this=this;

                //数据提交后端接口,新增地址
                $.post("/address/add",params,function (data) {
                    console.log(data)

                    //关闭弹窗
                    $("#myModal").modal('hide')

                    //刷新可选地址
                    _this.initAddressList();
                    _this.changeAddress();
                })
            }
            ,
            clearAddForm(){
                //打开新增地址表单时,清空表单数据
                this.provinceId=-1;
                this.cityId=-1;
                this.countyId=-1;
                this.detail="";
                this.phoneNumber="";
                this.receiver="";
                this.mailCode="";
                this.isDefault="";
            }
            ,
            changeAddress() {
                parent.app._data.addressId = this.defaultAddressId;
            }
            ,
            submitOrder(){
                //提交订单:  要结算的商品的id    收货地址id   当前用户信息(后台直接获取)
                console.log(this.productIds)
                console.log(this.defaultAddressId)

                let params={
                    productIds : this.productIds,
                    addressId : this.defaultAddressId
                }

                console.log(params)

                $.post("/order/createOrder",params,function (data) {
                    console.log(data)

                    if(data == "ok"){
                        //订单生成成功,前往我的订单页
                        console.log("订单生成成功")

                        location.href="/userorder.html";

                    }else{
                        alert(data)
                    }
                })
            }
        },
        created(){
            this.initCartAll();
            this.initAddressList();
            this.initProvinceList();
            this.changeAddress();
        }
    });


</script>


<!--<div th:insert="publicFooter"/>-->
<!--<script th:src="@{/publicFooter.js}"></script>-->


</body>
</html>